package net.lainiao.manager.control;

import com.github.pagehelper.PageInfo;
import net.lainiao.base.model.ShiroRole;
import net.lainiao.service.service.ShiroRoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.annotation.Resource;

/**
 * Created by Administrator on 2017/1/22.
 */
@Controller
@RequestMapping(value = "/role")
public class RoleControl {
    @Resource
    ShiroRoleService shiroRoleService;
    @RequestMapping(value = "/index")
    public String index(ModelMap modelMap){
        return index("1",modelMap);
    }

    @RequestMapping(value = "/index/{page}")
    public String index(@PathVariable String page,ModelMap modelMap){
        int pageIndex=Integer.parseInt(page);
        PageInfo<ShiroRole> pageInfo=shiroRoleService.getRolesForPage(pageIndex);
        modelMap.put("models", pageInfo.getList());
        modelMap.put("pageIndex", pageIndex);
        modelMap.put("pageTotal", pageInfo.getPages());
        return "/role/index";
    }

    @RequestMapping(value = "/add",method = RequestMethod.GET)
    public String add(){
        return "/role/add";
    }

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public String add(@ModelAttribute ShiroRole role, ModelMap modelMap){
        if(shiroRoleService.save(role)>0){
            return "redirect:/role/index";
        }
        modelMap.put("net.lainiao.zhihu.model",role);
        modelMap.put("mess","保存失败");
        return "/role/add";
    }

    @RequestMapping(value = "/edit/{roleId}",method = RequestMethod.GET)
    public String edit(@PathVariable String roleId,ModelMap modelMap){
        int rid=Integer.parseInt(roleId);
        ShiroRole role=shiroRoleService.getRoleForId(rid);
        modelMap.put("net.lainiao.zhihu.model",role);
        return "/role/edit";
    }

    @RequestMapping(value="/edit",method = RequestMethod.POST)
    public String edit(@ModelAttribute ShiroRole role,ModelMap modelMap){
        if(shiroRoleService.save(role)>0){
            return "redirect:/role/index";
        }
        modelMap.put("net.lainiao.zhihu.model",role);
        modelMap.put("mess","保存失败");
        return "/role/edit";
    }

    @RequestMapping(value = "/del/{idstr}",method = RequestMethod.GET)
    public String del(@PathVariable String idstr,ModelMap modelMap){
        int id=Integer.parseInt(idstr);
        if(shiroRoleService.del(id)>0){
            return "redirect:/role/index";
        }
        modelMap.put("mess","删除失败");
        return "/role/index";
    }
}
